package com.weimei.conversionTreasure.viewModel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.weimei.conversionTreasure.activity.VipActivity;
import com.weimei.conversionTreasure.http.Repository;
import com.weimei.conversionTreasure.http.entity.ComputerBean;
import com.weimei.conversionTreasure.http.entity.SheRuBean;
import com.weimei.conversionTreasure.utils.UserUtils;
import com.weimei.conversionTreasure.utils.ValidateUtils;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class UpDownVM extends BaseViewModel<Repository> {
    public UpDownVM(@NonNull Application application, Repository model) {
        super(application, model);
    }

    public ObservableField<String> originalAlcoholDegree = new ObservableField<>("");
    public ObservableField<String> originalWineMass = new ObservableField<>("");
    public ObservableField<String> targetAlcoholDegree = new ObservableField<>("");
    public ObservableField<String> highAlcoholDegree = new ObservableField<>("");

    public ObservableField<String> lowAlcoholDegree = new ObservableField<>("");

    public ObservableField<String>  result = new ObservableField<>("");
    public ObservableField<String>  result1 = new ObservableField<>("");

    public ObservableField<String> type = new ObservableField<>("0");

    public BindingCommand js=  new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (UserUtils.getFreeNum() <= 0) {
                if (!UserUtils.isVip()){
                    ToastUtils.showShort("您还不是会员，请先开通会员后再使用！！！");
                    startActivity(VipActivity.class);
                    return;
                }
            }
            if (type.get().equals("0")){
                up();
            }else {
                down();
            }




        }
    });

    private void up() {
        if (ValidateUtils.isEmptyString(originalAlcoholDegree.get())){
            ToastUtils.showShort("请输入原酒酒度");
            return;
        }
        if (ValidateUtils.isEmptyString(originalWineMass.get())){
            ToastUtils.showShort("请输入原酒质量");
            return;
        }
        if (ValidateUtils.isEmptyString(targetAlcoholDegree.get())){
            ToastUtils.showShort("请输入目标酒度");
            return;
        }
        if (ValidateUtils.isEmptyString(highAlcoholDegree.get())){
            ToastUtils.showShort("请输入高度酒酒度");
            return;
        }

        Map<String,String> map = new HashMap<>();
        map.put("originalAlcoholDegree",originalAlcoholDegree.get());
        map.put("originalWineMass",originalWineMass.get());
        map.put("targetAlcoholDegree",targetAlcoholDegree.get());
        map.put("highAlcoholDegree",highAlcoholDegree.get());
        model.degrees(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<SheRuBean>() {
                    @Override
                    public void onNext(SheRuBean sheRuBean) {
                        if (sheRuBean.issucc){
                            UserUtils.opreFree();
                            result.set(sheRuBean.getData().getHighAlcoholMass()+"");
                            result1.set(sheRuBean.getData().getTargetWineMass()+"");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void down() {
        if (ValidateUtils.isEmptyString(originalAlcoholDegree.get())){
            ToastUtils.showShort("请输入原酒酒度");
            return;
        }
        if (ValidateUtils.isEmptyString(originalWineMass.get())){
            ToastUtils.showShort("请输入原酒质量");
            return;
        }
        if (ValidateUtils.isEmptyString(targetAlcoholDegree.get())){
            ToastUtils.showShort("请输入目标酒度");
            return;
        }
        if (ValidateUtils.isEmptyString(lowAlcoholDegree.get())){
            ToastUtils.showShort("请输入低度酒酒度");
            return;
        }

        Map<String,String> map = new HashMap<>();
        map.put("originalAlcoholDegree",originalAlcoholDegree.get());
        map.put("originalWineMass",originalWineMass.get());
        map.put("targetAlcoholDegree",targetAlcoholDegree.get());
        map.put("lowAlcoholDegree",lowAlcoholDegree.get());
        model.elevations(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<SheRuBean>() {
                    @Override
                    public void onNext(SheRuBean sheRuBean) {
                        if (sheRuBean.issucc){
                            UserUtils.opreFree();
                            result.set(sheRuBean.getData().getLowAlcoholMass()+"");
                            result1.set(sheRuBean.getData().getTargetWineMass()+"");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
